package com.example.springbootecharts.controller;

import com.example.springbootecharts.model.Product;
import com.example.springbootecharts.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: xulinyi
 * @create: 2019-04-01 16:03
 **/
@Controller
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @GetMapping("/list")
    @ResponseBody
    public Map<String,List> findAll(){
        List<Product> list = productService.findAll();
        List<String> productNameList = list.stream().map(Product::getProductName).collect(Collectors.toList());
        List<Integer> numList = list.stream().map(Product::getNum).collect(Collectors.toList());
        Map<String,List> map=new HashMap<>();
        map.put("productName",productNameList);
        map.put("num",numList);
        return map;
    }

}
